package Persistencia;

import Modelo.Proveedor;
import java.sql.*;

public class AdmPersistenciaProveedor 
{
	private static AdmPersistenciaProveedor  instancia;
	
	public static AdmPersistenciaProveedor  getInstancia()
	{
		if (instancia == null)
			instancia = new AdmPersistenciaProveedor ();
		return instancia;
	}
	
	public void insert(Proveedor c)
	{
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try
		{
			PreparedStatement ps = con.prepareStatement("insert into examen03.dbo.Proveedor values(?,?)");
			ps.setString(1, c.getCuit());
			ps.setString(2, c.getNombre());
			
			
			ps.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch(Exception e)
		{
			System.out.println("Error en insert proveedor");
		}
	}
	public void delete(Proveedor c)
	{
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try
		{
			PreparedStatement ps = con.prepareStatement("delete from examen01.dbo.proveedores where cuit = ?");
			ps.setString(1,c.getCuit());
			
			ps.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch(Exception e)
		{
			System.out.println("Error en delete proveedor");
		}
	}
	public void update(Proveedor c)
	{
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try
		{
			PreparedStatement ps = con.prepareStatement("update examen01.dbo.proveedores set nombre = ?, where cuit = ?");
			ps.setString(1, c.getNombre());
			ps.setString(4,c.getCuit());
			
			ps.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		}
		catch(Exception e)
		{
			System.out.println("Error en update proveedor");
		}
	}
	public Proveedor select(String cuit)
	{
		Proveedor c = null;
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try
		{
			PreparedStatement ps = con.prepareStatement("select * from examen01.dbo.proveedores where dni = ?");
			ps.setString(1,cuit);
			
			ResultSet rs = ps.executeQuery();
			while (rs.next())
			{
				c = new Proveedor (rs.getString("nombre"),rs.getString("cuit"));
				c.setCuit(cuit);
			}
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return c;
		}
		catch(Exception e)
		{
			System.out.println("Error en update proveedor");
			return c;
		}
	}
}
